
import com.nokia.mid.ui.orientation.Orientation;
import com.pianoinhand.canvas.Button;
import com.pianoinhand.canvas.View;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;

public class UIMain extends Canvas
        implements CommandListener {

    public static Vector vecMusic;
    public UIKey[] arrUIKeyW;
    public UIKey[] arrUIKeyB;
    public int startX = 4;
    public static PlayMusic p;
    Command helpCommand;
    Command aboutCommand;
    Command exitCommand;
    static final int IDLE = 0;
    static final int EXIT = 4;
    public int BACKGROUND_COLOR = 0x002485db;
    Button btnMenu, btnExit;
    App midlet;

    public UIMain(final App mid) {
        midlet = mid;

        setFullScreenMode(true);
        vecMusic = new Vector();

        this.arrUIKeyW = new UIKey[7];
        try {
            p = new PlayMusic("/a4.mp3", 1, "audio/mpeg");
        } catch (Exception e) {
            e.printStackTrace();
        }
        int w = R.key_white_normal.getWidth();
        for (int i = 0; i < this.arrUIKeyW.length; i++) {
            this.arrUIKeyW[i] = new UIKey(this.startX + i * w, 38, 0, i);
        }

        this.arrUIKeyB = new UIKey[5];
        this.arrUIKeyB[0] = new UIKey(27, 38, 1, 0);
        this.arrUIKeyB[1] = new UIKey(93, 38, 1, 1);
        this.arrUIKeyB[2] = new UIKey(193, 38, 1, 2);
        this.arrUIKeyB[3] = new UIKey(256, 38, 1, 3);
        this.arrUIKeyB[4] = new UIKey(319, 38, 1, 4);

        this.helpCommand = new Command("help", 1, 1);
        this.aboutCommand = new Command("about", 1, 1);
        this.exitCommand = new Command("exit", 1, 1);
//        addCommand(this.helpCommand);
//        addCommand(this.aboutCommand);
//        addCommand(this.exitCommand);
//        setCommandListener(this);
        btnExit = new Button("", new Button.Listener() {
            public void onClick(Button b) {
                System.out.println("exiting...");
                midlet.exit();

            }
        });
        btnExit.setVisible(true);
        btnExit.setSize(44, 44);
        btnExit.setTop(2);
        btnExit.setRight(49);

        btnMenu = new Button("", new Button.Listener() {
            public void onClick(Button b) {
                Orientation.setAppOrientation(Orientation.ORIENTATION_PORTRAIT);
                mid.showMenuForm();
            }
        });

        btnMenu.setVisible(true);
        btnMenu.setSize(44, 44);
        btnMenu.setTop(2);
        btnMenu.setRight(394);


    }

    public void setBGColor(int color) {
        this.BACKGROUND_COLOR = color;
    }

    protected void paint(Graphics g) {
        clearScreen(g);
        g.setColor(BACKGROUND_COLOR);
        g.fillRect(0, 0, getWidth(), getHeight());
        Image imgMenu = null;
        try {
            imgMenu = Image.createImage("/menu_icon.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        btnMenu.renderImg(g, imgMenu);
        Image imgExit = null;
        try {
            imgExit = Image.createImage("/exit.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        btnExit.renderImg(g, imgExit);
        for (int i = 0; i < this.arrUIKeyW.length; i++) {
            this.arrUIKeyW[i].paint(g);
        }

        for (int i = 0; i < this.arrUIKeyB.length; i++) {
            this.arrUIKeyB[i].paint(g);
        }
    }

    public void pointerPressed(int x, int y) {
        btnMenu.handlePointerEvent(View.POINTER_PRESSED, x, y);
        btnExit.handlePointerEvent(View.POINTER_PRESSED, x, y);
        for (int i = 0; i < this.arrUIKeyB.length; i++) {
            if (this.arrUIKeyB[i].pointerPress(x, y)) {
                repaint();
                return;
            }
        }
        for (int i = 0; i < this.arrUIKeyW.length; i++) {
            if (this.arrUIKeyW[i].pointerPress(x, y)) {
                repaint();
                return;
            }
        }
    }

    public void pointerReleased(int x, int y) {
        btnMenu.handlePointerEvent(View.POINTER_RELEASED, x, y);
        btnExit.handlePointerEvent(View.POINTER_RELEASED, x, y);
        for (int i = 0; i < this.arrUIKeyB.length; i++) {
            this.arrUIKeyB[i].pointerRelease(x, y);
        }
        for (int i = 0; i < this.arrUIKeyW.length; i++) {
            this.arrUIKeyW[i].pointerRelease(x, y);
        }
        repaint();
    }

    public void clearScreen(Graphics g) {
        g.setColor(0);
        g.fillRect(0, 0, getWidth(), getHeight());
    }

    public void commandAction(Command c, Displayable arg1) {
        if (c == this.exitCommand) {
            App.app.notifyDestroyed();
        }

        if (c == this.aboutCommand) {
        }

        if (c == this.helpCommand) {
        }
    }

    public void showNextDisplayable(Form form) {
        Display.getDisplay(midlet).setCurrent(form);
    }
}
